// pages/shouquan/shouquan.js
const db = wx.cloud.database()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        openid: '',
        nickName: '',
        userPhoto: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let user=wx.getStorageSync('openid')
        var userinfo =wx.getStorageSync('userinfo')
        var nickname =wx.getStorageSync('nickname')
        if(user===''||userinfo===''||nickname===''){
        }
        else{
            wx.redirectTo({
                url: '../login/index',
            })
        }
    },
    UserInfo(ev) {
        wx.getUserProfile({
            desc: '用于获取微信信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                let userInfo = res.userInfo
                //console.log(userInfo);
                this.setData({
                    userPhoto: userInfo.avatarUrl,
                    nickName: userInfo.nickName
                })
                getApp().globalData.userinfo = userInfo.avatarUrl
                getApp().globalData.nickname = userInfo.nickName
                wx.cloud.callFunction({
                    name: 'getOpenID',
                    success: (res) => {
                        var usid = res.result.openid
                        //console.log(usid)
                        this.setData({
                            openid: res.result.openid,
                        })
                        getApp().globalData.openid = res.result.openid
                        wx.setStorageSync('openid', res.result.openid)
                        wx.setStorageSync('userinfo', this.data.userPhoto)
                        wx.setStorageSync('nickname', this.data.nickName)
                        db.collection("user").where({ openid: res.result.openid }).get().then(res => {
                            //console.log(res.data)
                            if (res.data == '') {
                                db.collection("user").add({
                                    data: {
                                        openid: this.data.openid,
                                        userphoto: this.data.userPhoto,
                                        nickname: this.data.nickName,
                                        _createTime: Date.parse(new Date()),
                                    }
                                }).then(res => {
                                    wx.showToast({
                                        title: '授权成功',
                                        icon: 'success',
                                        duration: 2000,
                                        success: function () {
                                            setTimeout(function () {
                                                wx.navigateTo({
                                                    url: '../login/index',
                                                })
                                            }, 2000)
                                        }
                                    })
                                })
                            }
                            else {
                                wx.showToast({
                                    title: '授权成功',
                                    icon: 'success',
                                    duration: 2000,
                                    success: function () {
                                        setTimeout(function () {
                                            wx.navigateTo({
                                                url: '../regist/index',
                                            })
                                        }, 2000)
                                    }
                                })
                            }
                        })

                    },
                    fail(res) {
                        console.log("获取失败", res);
                    }
                })
            }
        })
    },

})